
import 'package:flutter_keep_book/core/extensions/map.dart';

class UserInfo {
  UserInfo();

  int userId = 0;
  String mobile = '';
  String nickName = '';
  /// 性别0/1/2（未设置/男/女）
  int gender = 0;
  String birthday = '';
  int coin = 0;
  String avatar = '';

  ///是否新用户
  bool isNewUser = false;

  ///是否新用户
  bool isShowNewcomerGift = false;

  ///新手福利json弹窗
  String popupJson = '';

  String get genderText {
    switch (gender) {
      case 1:
        return '男';
      case 2:
        return '女';
      default:
        return '未设置';
    }
  }

  static UserInfo? fromJson(dynamic json) {
    if (json is! Map || json.isEmpty) {
      return null;
    }
    return UserInfo()
      ..userId = json.intValue('user_id') ?? 0
      ..mobile = json.stringValue('mobile') ?? ''
      ..nickName = json.stringValue('nick_name') ?? ''
      ..gender = json.intValue('gender') ?? 0
      ..birthday = json.stringValue('birthday') ?? ''
      ..coin = json.intValue('coin') ?? 0
      ..avatar = json.stringValue('avatar') ?? ''
      ..isNewUser = json.boolValue('is_new_user') ?? false
      ..isShowNewcomerGift = json.boolValue('is_show_newcomer_gift') ?? false
      ..popupJson = json.stringValue('popup_json') ?? '';
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'user_id': userId,
        'mobile': mobile,
        'nick_name': nickName,
        'gender': gender,
        'birthday': birthday,
        'coin': coin,
        'avatar': avatar,
        'is_new_user': isNewUser,
        'is_show_newcomer_gift': isShowNewcomerGift,
        'popup_json': popupJson,
      };
}
